package com.atguigu.juc_day02;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * java
 * 2022/9/8 20 06
 */
class Prent1_52AndA_Z{
    private int num = 1;
    private Lock lock = new ReentrantLock ();
    private Condition condition1 = lock.newCondition ();
    private Condition condition2 = lock.newCondition ();

    public void print1_52() throws InterruptedException {
        lock.lock ();
        for (int i = 1; i <= 52; i++) {
            while (num%3==0){
                condition1.await ();
            }
            System.out.print (i);
            num++;
            condition2.signal ();
        }
        lock.unlock ();
    }
    public void printA_B() throws InterruptedException {
        lock.lock ();
        for (char i = 'A'; i <= 'Z'; i++) {
            while (num%3!=0){
                condition2.await ();
            }
            System.out.println (i);
            num++;
            condition1.signal ();
        }
    }
}

public class ProdConsumerDemo3 {
    public static void main (String[] args) {
        Prent1_52AndA_Z prent1_52AndA_z = new Prent1_52AndA_Z ();
        new Thread (()->{
            try {
                prent1_52AndA_z.print1_52 ();
            } catch (InterruptedException e) {
                throw new RuntimeException (e);
            }
        }).start ();
        new Thread (()->{
            try {
                prent1_52AndA_z.printA_B ();
            } catch (InterruptedException e) {
                throw new RuntimeException (e);
            }
        }).start ();
    }
}
